<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>19.迭代器自定义遍历对象</title>
</head>
<body>
    <script>
        // 声明一个对象
        const banji = {
            name:'终极一班',
            stus:['ming','ning','tian','knight'],
            [Symbol.iterator](){
                //声明索引变量
                let index = 0;
                let _this = this;
                return {
                    next:function(){
                        if(index < _this.stus.length){
                            const result = {value:_this.stus[index],done:false};
                            // 索引下标自增
                            index++;
                            //返回结果
                            return result
                        }else{
                            return {value:'undefined',done:true}
                        }    
                    },
                };
            }
        }

        //遍历这个对象 banji.stus
        for (const v of banji) {
            console.log(v) //Uncaught TypeError: banji is not iterable
        }

        // 按照自己意愿，不直接操作对象属性
        for (const v of banji) {
            console.log(v) 
        }
    </script>
</body>
</html>